package com.WJY.dao;

import com.WJY.pojo.Teacher;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidPooledConnection;
import lombok.Data;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@Data
public class TeacherDaoImpl implements TeacherDao {

    @Override
    public Teacher queryTeacherById(int id) {
        ApplicationContext context = new ClassPathXmlApplicationContext("bean.xml");
        DruidDataSource dataSource = context.getBean("dataSource", DruidDataSource.class);
        DruidPooledConnection conn = null;
        Statement st = null;
        ResultSet rs = null;
        Teacher teacher = null;
        try {
            conn = dataSource.getConnection();
            st = conn.createStatement();
            rs = st.executeQuery("select * from teacher where tId = "+id);
            while (rs.next()) {
                teacher = new Teacher(rs.getInt("tId"), rs.getString("tName"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return teacher;
    }
}
